import 'package:jtsell_app/database/database_helper.dart';

class CartItemEntity {
  static final String _TABLE_NAME = "cart_table";

  String productID;
  String productName;
  int buyLimit;
  bool isSelected;
  bool isDeleted;
  double price;
  String imageUrl;
  int buyCount;

  CartItemEntity(
      {this.buyLimit,
      this.productID,
      this.isDeleted,
      this.price,
      this.imageUrl,
      this.buyCount,
      this.isSelected,
      this.productName});

  CartItemEntity.fromJson(Map<String, dynamic> json) {
    buyLimit = json['buyLimit'];
    productID = json['productID'];
    isDeleted = json['isDeleted'] == 1;
    price = json['price'];
    imageUrl = json['imageUrl'];
    buyCount = json['buyCount'];
    isSelected = json['isSelected'] == 1;
    productName = json['productName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['buyLimit'] = this.buyLimit;
    data['productID'] = this.productID;
    data['isDeleted'] = this.isDeleted ? 1 : 0;
    data['price'] = this.price;
    data['imageUrl'] = this.imageUrl;
    data['buyCount'] = this.buyCount;
    data['isSelected'] = this.isSelected ? 1 : 0;
    data['productName'] = this.productName;
    return data;
  }

  ///
  /// 查询所有数据
  ///
  static Future<List<CartItemEntity>> getAllList() async {
    DatabaseHelper databaseHelper = await DatabaseHelper.getInstance();
    List<Map<String, dynamic>> listMap = await databaseHelper.getTotalList(_TABLE_NAME);
    databaseHelper.close();
    return listMap.map((map) {
      return CartItemEntity.fromJson(map);
    }).toList();
  }

  ///
  /// 保存到数据库
  ///
  Future<int> save() async {
    DatabaseHelper databaseHelper = await DatabaseHelper.getInstance();
    int count = await databaseHelper.saveItem(_TABLE_NAME, toJson());
    databaseHelper.close();
    return count;
  }

  Future<int> updateBuyCount() async {
    DatabaseHelper databaseHelper = await DatabaseHelper.getInstance();
    Map<String, dynamic> map = {"buyCount": this.buyCount};
    int count = await databaseHelper
        .updateItem(_TABLE_NAME, map, where: " productID = ? ", whereArgs: [this.productID]);
    databaseHelper.close();
    return count;
  }

  Future<int> delete() async {
    DatabaseHelper databaseHelper = await DatabaseHelper.getInstance();
    int count = await databaseHelper.deleteItemByID(_TABLE_NAME, "productID", this.productID);
    databaseHelper.close();
    return count;
  }

  ///
  /// 测试数据
  ///
  static List<CartItemEntity> testData() {
    List<CartItemEntity> listCartItem = List();
    for (int i = 0; i < 20; i++) {
      CartItemEntity item = CartItemEntity();
      item.productName = "商品商品商品商品商品商品商品商品 $i";
      item.buyLimit = 8;
      item.buyCount = 5;
      item.imageUrl = "http://images.baixingliangfan.cn/compressedPic/20190109173820_1556.jpg";
      item.isSelected = true;
      item.isDeleted = false;
      item.price = 9.8;

      listCartItem.add(item);
    }

    return listCartItem;
  }
}
